import os
import sqlite3

__author__ = 'gongqf'
print os.getcwd()
if os.path.exists(r'e:\web\flask\topshipping'):
    os.chdir(r'e:\web\flask\topshipping')
elif os.path.exists(r'l:\web\flask\topshipping'):
    os.chdir(r'l:\web\flask\topshipping')
elif os.path.exists(r'g:\web\flask\topshipping'):
    os.chdir(r'g:\web\flask\topshipping')

conn=sqlite3.connect('topshipping.db3')
conn.text_factory = str
countrys=open(r'static\c.txt').readlines()
for country in countrys:
    cs=country.strip('\r\n')
    c=cs.split(',')
#    if c[0]<>c[3]:
#        print cs
    fc=conn.execute("select * from country where countryname=?",(c[5],)).fetchone()
#    print fc
    if fc==None:
        print c[5]
        conn.execute("insert into country(country2code,country3code,countryuncode,countrynameen,countryname) values(?,?,?,?,?)",(c[0],c[1],c[2],c[4],c[5]))
    else:
        conn.execute("update country set country2code=?,country3code=?,countryuncode=?,countrynameen=? where countryname=?",(c[0],c[1],c[2],c[4],c[5]))

#    conn.execute('replace into country(countryname) values(?)',(country.decode('gbk').strip('\n'),))
conn.commit()